﻿using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinServer
{
    /// 第三步、接收过滤器(ReceiveFilter) 接收过滤器(ReceiveFilter)用于将接收到的二进制数据转化成请求实例(RequestInfo)。
    /// 需要实现接口 IReceiveFilter 

    public class HansReceiveFilter : IReceiveFilter<HansRequestInfo>
    {

        /// 该接收过滤器已缓存数据的长度:这里用过滤后的数据长度【BodyBuffer.Length】

        public int leftBufferSize;

        /// 字符编码

        public Encoding Encoder = Encoding.GetEncoding("gbk");

        /// 开始标记 如："!Start"    

        public string StartMark = "!Start";

        /// 结束标记 如："$End"

        public string EndMark = "$End";

        public HansReceiveFilter(Encoding encoder, string startMark, string endMark)
        {
            Encoder = encoder;

            StartMark = startMark;

            EndMark = endMark;

        }
        public HansReceiveFilter( string startMark, string endMark)
        {   
            StartMark = startMark;

            EndMark = endMark;

        }

        /// <returns>当你在接收缓冲区中找到一条完整的请求时，你必须返回一个你的请求类型的实例.当你在接收缓冲区中没有找到一个完整的请求时, 你需要返回 NULL.当你在接收缓冲区中找到一条完整的请求, 但接收到的数据并不仅仅包含一个请求时，设置剩余数据的长度到输出变量 "rest". SuperSocket 将会检查这个输出参数 "rest", 如果它大于 0, 此 Filter 方法 将会被再次执行, 参数 "offset" 和 "length" 会被调整为合适的值.</returns>

        public HansRequestInfo Filter(byte[] readBuffer, int offset, int length, bool toBeCopied, out int rest)
        {

            //因接收的数据如果不匹配内容，则舍弃之，因此都为0
            rest = 0;

            byte[] startMarkBuffer = Encoder.GetBytes(StartMark);

            byte[] endMarkBuffer = Encoder.GetBytes(EndMark);

            if (length < startMarkBuffer.Length + endMarkBuffer.Length)//没有数据

                return null;

            byte[] data = new byte[length];

            Buffer.BlockCopy(readBuffer, offset, data, 0, length);

            string receiveStartMark = Encoder.GetString(data, 0, startMarkBuffer.Length);

            string receiveEndMark = Encoder.GetString(data, length - endMarkBuffer.Length, endMarkBuffer.Length);

            HansReceiveData receiveData = new HansReceiveData();

            receiveData.StartMark = StartMark;

            receiveData.Key = Guid.NewGuid().ToString("B");

            receiveData.BodyBuffer = new byte[length - startMarkBuffer.Length - endMarkBuffer.Length];

            Buffer.BlockCopy(data, startMarkBuffer.Length, receiveData.BodyBuffer, 0, length - startMarkBuffer.Length - endMarkBuffer.Length);

            receiveData.EndMark = EndMark;

            receiveData.BodyString = Encoder.GetString(receiveData.BodyBuffer);

            leftBufferSize = length - startMarkBuffer.Length - endMarkBuffer.Length;

            //如果开始标记 或者 结束标记 不匹配设定值，则返回null

            if (!receiveStartMark.Equals(StartMark) || !receiveEndMark.Equals(EndMark))

                return null;

            return new HansRequestInfo(receiveData.Key, receiveData);

        }

        /// 该接收过滤器已缓存数据的长度


        public int LeftBufferSize

        {
            get { return leftBufferSize; }

        }
        /// 当下一块数据收到时，用于处理数据的接收过滤器实例; 

        public SuperSocket.SocketBase.Protocol.IReceiveFilter<HansRequestInfo> NextReceiveFilter
        {
            get { return this; }

        }     

        /// 重设接收过滤器实例到初始状态

        public void Reset()
        {


        }


        public SuperSocket.SocketBase.Protocol.FilterState State
        {
            get;
            private set;

        }

    }
}
